package Math;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class _593_ValidSquare {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        Map<Double, Double> maps = new HashMap<>();
        double edge1 = Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2);
        double edge2 = Math.pow(p1[0] - p3[0], 2) + Math.pow(p1[1] - p3[1], 2);
        double edge3 = Math.pow(p1[0] - p4[0], 2) + Math.pow(p1[1] - p4 [1], 2);
        double edge4 = Math.pow(p2[0] - p3[0], 2) + Math.pow(p2[1] - p3 [1], 2);
        double edge5 = Math.pow(p2[0] - p4[0], 2) + Math.pow(p2[1] - p4 [1], 2);
        double edge6 = Math.pow(p3[0] - p4[0], 2) + Math.pow(p3[1] - p4 [1], 2);
        maps.put(edge1, maps.getOrDefault(edge1, 0.0) + 1);
        maps.put(edge2, maps.getOrDefault(edge2, 0.0) + 1);
        maps.put(edge3, maps.getOrDefault(edge3, 0.0) + 1);
        maps.put(edge4, maps.getOrDefault(edge4, 0.0) + 1);
        maps.put(edge5, maps.getOrDefault(edge5, 0.0) + 1);
        maps.put(edge6, maps.getOrDefault(edge6, 0.0) + 1);

        if (maps.size() != 2) {
            return false;
        }
        List<Double> list = new ArrayList<>();
        for (Map.Entry<Double, Double> e : maps.entrySet()) {
            list.add(e.getKey());
        }

        if (maps.get(list.get(0)) == 4 || maps.get(list.get(1)) == 4) {
            if (maps.get(list.get(0)) == 4 && list.get(0) < list.get(1)) {
                return true;
            } else if (maps.get(list.get(1)) == 4 && list.get(1) < list.get(0)) {
                return true;
            }
        } else {
            return false;
        }
        return false;
    }
}
